#include <bits/stdc++.h>
using namespace std;
const int maxn = 7+5;
const int maxm = 1<<8-1+5;
int pos[maxn], seq[maxn];
char stat[maxn];

void init(){
    memset(pos, 0, sizeof(pos));
    memset(seq, 0, sizeof(seq));
    memset(stat, 0, sizeof(stat));
}
int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    int N, M, T = 0;
    string in;
    while(getline(cin, in) && in[0]!='0'){
        ++T; init();
        N = in[0] - '0';
        getline(cin, in);
        stringstream ss(in);
        string s; int t = 1;
        while(ss >> s){
            pos[s[1]-'0'] = t++;
        }
        getline(cin, in);
        for (int i=1, j=1<<N; i-1 < in.size(); ++i,++j) {
            stat[j] = in[i-1];
        }
        getline(cin, in); M = in[0]-'0';
        string ans;
        cout << "S-Tree #" << T << ":\n";
        for (int i = 0; i < M; ++i) {
            getline(cin, in);
            for (int j = 1; j-1 < in.size(); ++j) {
                seq[pos[j]] = (in[j-1]-'0');
            }
            t = 1;
            for (int k = 1; k <= N; ++k) {
                if(seq[k]) t = t*2+1;
                else t *= 2;
            }
            ans.push_back(stat[t]);
        }
        cout << ans << "\n";
        cout << "\n";
    }
    return 0;
}